﻿using System;
using System.Collections.Generic;
using System.Linq;
using WEB.BLL;
using WEB.BLL.Entities;
using System.Web.UI;
using System.Xml;
using System.Xml.Xsl;
using System.Web.Security;

namespace WEB.Core.Controls
{
    public partial class BasePage : Page, IBasePage
    {
        #region
        private string _screenId = "";
        private string _formTitle = "";
        private bool _CheckDashboard = false;
        private bool _CheckPermissionControl = false;
        private bool _CheckPermissionHidden = false;
        #endregion

        #region
        public string ScreenId
        {
            get { return _screenId; }
            set { _screenId = value; }
        }
        public string FormTitle
        {
            get { return _formTitle; }
            set { _formTitle = value; }
        }
        public string HostName
        {
            get{ return String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority.ToLower()); }
        }
        public bool IsDashboard
        {
            get
            {
                string _Dashboard = Request.QueryString["Dashboard"];
                if (_Dashboard != null && _Dashboard.ToLower() == "true")
                    return true;
                return false;
            }
        }
        public int Width
        {
            get
            {
                try
                {
                    return Util.Util.Obj2Int(Request.QueryString["Width"]);
                }
                catch
                {
                    return 0;
                }
            }
        }
        public int Height
        {
            get
            {
                try
                {
                    return Util.Util.Obj2Int(Request.QueryString["Height"]);
                }
                catch
                {
                    return 0;
                }
            }
        }
        protected static UserEntity CurrentUser
        {
            get
            {
                if (WEBContext.Current.User == null) return new UserEntity();
                return WEBContext.Current.User;
            }
        }
        #endregion

        #region Constructor
        public BasePage(string screenId): this(screenId, true)
        {
        }

        public BasePage(string screenId, bool HaveDocumentReady)
            : this(screenId, HaveDocumentReady, true, true, true)
        {
        }

        public BasePage(string screenId, bool CheckDashboard, bool CheckPermissionControl, bool CheckPermissionHidden)
            : this(screenId, true, CheckDashboard, CheckPermissionControl, CheckPermissionHidden)
        {
        }

        public BasePage(string screenId, bool HaveDocumentReady, bool CheckDashboard, bool CheckPermissionControl, bool CheckPermissionHidden)
        {
            _screenId = screenId;
            CheckPermission();
            CreateResourceManager(HaveDocumentReady);
            _CheckDashboard = CheckDashboard;
            _CheckPermissionControl = CheckPermissionControl;
            _CheckPermissionHidden = CheckPermissionControl;
            //6ECFE9FF08ABA25FBFF
            //string MachineID = License.GetMachineID("D");
        }
        #endregion

        #region
        protected virtual void Page_Load(object sender, EventArgs e)
        {
            if (CurrentUser == null)
            {
                FormsAuthentication.SignOut();
                FormsAuthentication.RedirectToLoginPage();
                return;
            }
            if (CurrentUser.RoleId == -1 && !ScreenId.Equals(WEB.Core.Util.SCREENS.BREIFCVDETAIL) && !ScreenId.Equals(WEB.Core.Util.SCREENS.CV))
                Response.Redirect("~/CV.aspx");
            AddPageScript();
            AddListControl();
            if (!IsPostBack)
                NotPostBack();
            CheckDashboard();
            CheckPermissionControl();
            CheckPermissionHidden();
        }

        /// <summary>
        /// Chạy các hàm khi khi Page Load nếu trang không phải là PostBack
        /// </summary>
        protected virtual void NotPostBack()
        {
        }

        protected virtual void AddListControl()
        {
        }
        #endregion
    }
}
